package com.vogoal.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class Html5FileUpload extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Html5FileUpload() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		DiskFileItemFactory fif=new DiskFileItemFactory();
		fif.setSizeThreshold(1024*1024);
		ServletFileUpload sfu=new ServletFileUpload(fif);
		sfu.setSizeMax(1024*1024*1024);
		List items=null;
		try {
			items=sfu.parseRequest(request);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator iter=items.iterator();
		String filename="";
		String filesize="";
		String filetype="";
		FileItem fi=null;
		while (iter.hasNext()) {
		    FileItem item = (FileItem) iter.next();
		    if (item.isFormField()) {
		        String name=item.getFieldName();
		        if(name.equals("filename")==true){
		        	filename=item.getString("UTF-8");
		        }else if(name.equals("filesize")==true){
		        	filesize=item.getString("UTF-8");
		        }else if(name.equals("filetype")==true){
		        	filetype=item.getString("UTF-8");
		        }
		    } else {
		    	fi=item;
		    }
		}
		File f1=new File("/home/zhangyuotng926/"+filename);
		try {
			fi.write(f1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("你上传了一个文件到服务器，下面将核实这些信息：");
		out.println("1、文件名："+filename);
		out.println("2、文件大小："+filesize);
		out.println("3、文件MIME类型："+filetype);
		out.println("如果信息全部正确，说明文件成功上传了！");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
